/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Date.prototype.format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份   
        "d+": this.getDate(), //日   
        "h+": this.getHours(), //小时   
        "m+": this.getMinutes(), //分   
        "s+": this.getSeconds(), //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt; 
}

//时间格式转换为时间戳
function js_strto_time(time) {
    var new_str = time.replace(/:/g, '-');
    new_str = new_str.replace(/ /g, '-');
    new_str = new_str.replace(/\//g, '-');
    var arr = new_str.split("-");
    var datum = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4], arr[5]));
    return strtotime = datum.getTime() / 1000;
}
//转换为正常时间格式
function tr_time(str) {
    str = str.split(" ");
    var yymmdd = str[0];
    var hh = str[1].split(":")[0];
    var mi = str[1].split(":")[1];
    var astr = str[2];
    if (astr == "PM") {
        hh = 12 + parseInt(hh);
    }
    return yymmdd + " " + hh + ":" + mi + ":00";
}
//时间戳数据转换为时间格式
function js_date_time(unixtime) {
    var timestr = new Date(parseInt(unixtime)*1000);
    var year= timestr.getFullYear(); 
    var month= timestr.getMonth()+1; 
    var date= timestr.getDate(); 
    var hour= timestr.getHours(); 
    var minute= timestr.getMinutes(); 
    var second= timestr.getSeconds(); 
    var datetime = year+"/"+month+"/"+date+" "+hour+":"+minute+":"+second; 
    return datetime;
}

//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}

var base64 = new Base64();
var type = getQueryString("type");
var DepotID = getQueryString("DepotID");
var DepotName = getQueryString("DepotName");
DepotName = base64.decode(DepotName);

var PolicyObj = {};

function save() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    
    var comment = $(".policy_desc").val();
    PolicyObj.R_Type = $(".deviceInfoType").val();
    PolicyObj.R_Freq = $(".ReportPeriod").val();
    
    var policy = JSON.stringify(PolicyObj);
        
        $.ajax({
            type: "POST",
            async: false,
            url: "../../action/policy/create.do",
            data: {comment: comment, policy: policy, type: type, instance: DepotName},
            dataType: "json",
            success: function (data) {
                if (data.result == "true") {
                    alert("添加成功！");
                    previous();
                } else {
                    alert("添加失败!")
                }
            }
        });
}

function edit() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    
    var comment = $(".policy_desc").val();
    PolicyObj.R_Type = $(".deviceInfoType").val();
    PolicyObj.R_Freq = $(".ReportPeriod").val();
    
    var policy = JSON.stringify(PolicyObj);
        
        $.ajax({
            type: "POST",
            async: false,
            url: "../../action/policy/update.do",
            data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
            dataType: "json",
            success: function (data) {
                if (data.result == "true") {
                    alert("修改成功！");
                    previous();
                } else {
                    alert("修改失败!")
                }
            }
        });
}

function showData(resp) {
    var messageType = resp.MessageType;
    var messageNo = resp.MessageNo;
    var instanceName = resp.Instance;
    var comment = resp.Comment;
    PolicyObj = resp.Content;
    
    $(".policy_desc").val(comment);
    $(".deviceInfoType").val(PolicyObj.R_Type);
    $(".ReportPeriod").val(PolicyObj.R_Freq);
}

/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");
    
    showData(souData);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}